<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Contacts_c extends CI_Controller {
        
        function __construct() {
            parent::__construct();
            $this->load->model(array('client_m', 'coordinator_m', 'supplier_m'));
        }
        
        function index() {
            $data['content'] = 'pages/contacts/contacts';
            $data['title'] = 'Contacts';
            $data['current_page'] = 'contacts-page';
            $data['back_to'] = 'contacts';
            $data['employees'] = $this->coordinator_m->get_employees($this->session->userdata('accid'));
            $this->load->view('includes/template', $data);
        }
        
        function clients_ajax(){
            echo json_encode($this->client_m->get_clients());
        }
        
        function save_client() {
            $client_info = array(
                'client_fname' => $this->input->post('fname'),
                'client_lname' => $this->input->post('lname'),
                'client_address' => $this->input->post('address'),
                'client_num' => $this->input->post('contact_no'),
                'client_email' => $this->input->post('email'),
                'client_pw' => sha1($this->generate_password()),
            );
            $status = '';
            $client = '';
            $client_id = $this->input->post('client_id');
            if($this->input->post('action')==='0'){
                $id = $this->client_m->insert_client($client_info);
                if($id){
                    $status = 'success';
                    $client = $this->client_m->get_client($id);
                }else{
                    $status = 'error';
                    $client = '';
                }
            }elseif($this->input->post('action')==='1'){
                $id = $this->client_m->update_client($client_id, $client_info);
                if($id > 0){
                    $status = 'success';
                    $client = $this->client_m->get_client($client_id);
                }else{
                    $status = 'error';
                    $client = '';
                }
            }
            echo json_encode(array('status' => $status, 'client' => $client));
        }
        
        function view_client($id) {
            $client = $this->client_m->get_client($id);
            echo json_encode($client);
        }
        
        function delete_client() {
            $this->client_m->delete_client($this->input->post('client_id'));
        }
        
        function employees_ajax() {
            echo json_encode($this->coordinator_m->get_employees($this->session->userdata('accid')));
        }
        
        function save_employee() {
            $emp_info = array(
                'lname' => $this->input->post('lname'),
                'fname' => $this->input->post('fname'),
                'mname' => $this->input->post('mname'),
                'bday' => $this->input->post('bday'),
                'gender' => $this->input->post('gender'),
                'coord_address' => $this->input->post('address'),
                'coord_num' => $this->input->post('contact_no'),
                'coord_email' => $this->input->post('email'),
                'coord_pw' => sha1($this->generate_password()),
                'emp_date' => $this->input->post('emp_date'),
                'status' => 'active',
            );
            $status = '';
            $emp = '';
            $emp_id = $this->input->post('emp_id');
            if($this->input->post('action')==='0'){
                $id = $this->coordinator_m->insert_emp($emp_info);
                if($id){
                    $status = 'success';
                    $emp = $this->coordinator_m->get_employee($id);
                }else{
                    $status = 'error';
                    $emp = '';
                }
            }elseif($this->input->post('action')==='1'){
                $id = $this->coordinator_m->update_emp($emp_id, $emp_info);
                if($id > 0){
                    $status = 'success';
                    $emp = $this->coordinator_m->get_employee($emp_id);
                }else{
                    $status = 'error';
                    $emp = '';
                }
            }
            echo json_encode(array('status' => $status, 'emp' => $emp));
        }
        
        function view_employee($id) {
            $emp = $this->coordinator_m->get_employee($id);
            echo json_encode($emp);
        }
        
        function delete_employee() {
            $this->coordinator_m->delete_employee($this->input->post('emp_id'));
        }
        
        function supplier($msg = '') {
            $data['content'] = 'pages/contacts/supplier';
            $data['title'] = 'Supplier';
            $data['current_page'] = 'supplier-page';
            $data['class'] = '';
            $data['back_to'] = 'contacts';
            $data['msg'] = $msg;
            $data['supplier'] = $this->supplier_m->get_all_supplier();
            $this->load->view('includes/template', $data);
        }
        
        function supplier_ajax(){
            echo json_encode($this->supplier_m->get_all_supplier());
        }
        
        function save_supplier() {
            $supplier_info = array(
                'company' => $this->input->post('cname'),
                'contact_person' => $this->input->post('contact_person'),
                's_num' => $this->input->post('contact_no'),
                's_email' => $this->input->post('email'),
                'services' => $this->input->post('services'),
                's_address' => $this->input->post('address'),
                's_status' => 'active',
            );
            $status = '';
            $supplier = '';
            $supplier_id = $this->input->post('supplier_id');
            if($this->input->post('action')==='0'){
                $id = $this->supplier_m->insert_supplier($supplier_info);
                if($id){
                    $status = 'success';
                    $supplier = $this->supplier_m->get_supplier($id);
                }else{
                    $status = 'error';
                    $supplier = '';
                }
            }elseif($this->input->post('action')==='1'){
                $id = $this->supplier_m->update_supplier($supplier_id, $supplier_info);
                if($id > 0){
                    $status = 'success';
                    $supplier = $this->supplier_m->get_supplier($supplier_id);
                }else{
                    $status = 'error';
                    $supplier = '';
                }
            }
            echo json_encode(array('status' => $status, 'supplier' => $supplier));
        }
        
        function view_supplier($id) {
            echo json_encode($this->supplier_m->get_supplier($id));
        }
        
        function update_supplier() {
            $this->form_validation->set_rules('cname', 'Company Name', 'required');
            $this->form_validation->set_rules('service', 'Service', 'required');
            $this->form_validation->set_rules('contact_person', 'Contact Person', 'required');
            $this->form_validation->set_rules('contact_no', 'Contact No.', 'required|numeric');
            $this->form_validation->set_rules('address', 'Address', 'required');
            $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
            $this->form_validation->set_rules('address', 'Address', 'required');
            
            if($this->form_validation->run() == FALSE){
                
            }else{
                $supplier_info = array(
                    'company_name' => $this->input->post('cname'),
                    'contact_person' => $this->input->post('contact_person'),
                    'contact_no' => $this->input->post('contact_no'),
                    'email' => $this->input->post('email'),
                    'service' => $this->input->post('service'),
                    'address' => $this->input->post('address'),
                    'supplier_since' => $this->input->post('supplier_since'),
                );
                $supplier_id = $this->input->post('supplier_id');
                
                $this->supplier_m->update_supplier($supplier_id, $supplier_info);
                $this->supplier('<p class="success">Supplier <b>'.$this->input->post('cname').'</b> successfully updated.</p>');
            }
        }
        
        function delete_supplier() {
            $this->supplier_m->delete_supplier($this->input->post('supplier_id'));
        }
        function generate_password() {
            // generate random characters
            $rand_no = substr(str_shuffle('0123456789'), 0, 3);
            $rand_sletter = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 3);
            $rand_bletter = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 3);
            $pass = str_shuffle($rand_no . $rand_sletter . $rand_bletter);
            return $pass;
        }
    }
